﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using CarbonExchange.AmeeQuery;
using System.Xml;
using System.Xml.Linq;

namespace CarbonExchange.Tests
{
    [TestClass]
    public class AmeeQueryTests : BaseTest
    {
        [TestMethod]
        public void AuthenticatePassesWithGapUsernameAndPassword()
        {
            using (CarbonExchange.AmeeQuery.AmeeCommand cmd = new CarbonExchange.AmeeQuery.AmeeCommand())
            {
                Assert.IsTrue(cmd.Authenticate());
            }
        }

        [TestMethod]
        public void AuthenticateFailesWithIncorrectUsername()
        {
            IAmeeSettingsReader settings = new AmeeSettings.IncorrectUserNameSettings();
            using (CarbonExchange.AmeeQuery.AmeeCommand cmd = new CarbonExchange.AmeeQuery.AmeeCommand(settings))
            {
                Assert.IsFalse(cmd.Authenticate());
            }
        }

        [TestMethod]
        public void AuthenticateFailesWithIncorrectPassword()
        {
            IAmeeSettingsReader settings = new AmeeSettings.IncorrectPasswordSettings();
            using (CarbonExchange.AmeeQuery.AmeeCommand cmd = new CarbonExchange.AmeeQuery.AmeeCommand(settings))
            {
                Assert.IsFalse(cmd.Authenticate());
            }
        }

        [TestMethod]
        public void AuthenticateStoresKeyCorrectlyAndAllowsCommandToBeSent()
        {
            using (CarbonExchange.AmeeQuery.AmeeCommand cmd = new CarbonExchange.AmeeQuery.AmeeCommand())
            {
                Assert.IsTrue(cmd.Authenticate());
            }
            //now use incorrext username but as key generated should not be problem
            IAmeeSettingsReader settings = new AmeeSettings.IncorrectPasswordSettings();
            using (CarbonExchange.AmeeQuery.AmeeCommand cmd = new CarbonExchange.AmeeQuery.AmeeCommand(settings))
            {
                Assert.IsNotNull(cmd.SendXmlGet("profiles"));
            }

        }

        [TestMethod]
        public void SendXmlGetReturnsXDocument()
        {
            using (CarbonExchange.AmeeQuery.AmeeCommand cmd = new CarbonExchange.AmeeQuery.AmeeCommand())
            {
                Assert.IsNotNull(cmd.SendXmlGet("profiles"));
            }
        }

        [TestMethod]
        public void SendXmlGetReturnsXDocumentWhichCanBeRead()
        {
            using (CarbonExchange.AmeeQuery.AmeeCommand cmd = new CarbonExchange.AmeeQuery.AmeeCommand())
            {
                XDocument reader = cmd.SendXmlGet("profiles");
                Assert.IsNotNull(reader);
                var tmp = (from child in reader.Root.Elements() select child).ToArray();
            }
        }

        [TestMethod]
        public void SendJsonGetReturnsJObject()
        {
            using (CarbonExchange.AmeeQuery.AmeeCommand cmd = new CarbonExchange.AmeeQuery.AmeeCommand())
            {
                Assert.IsNotNull(cmd.SendJsonGet("profiles"));
            }
        }

        [TestMethod]
        public void SendJsonGetReturnsJObjectWhichCanBeRead()
        {
            using (CarbonExchange.AmeeQuery.AmeeCommand cmd = new CarbonExchange.AmeeQuery.AmeeCommand())
            {
                Newtonsoft.Json.Linq.JObject reader = cmd.SendJsonGet("profiles");
                Assert.IsNotNull(reader);
                var tmp = (from child in reader.Root.Children() select child).ToArray();
            }
        }
    }
}
